package umn.angrypenguins.monopoly;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import umn.angrypenguins.monopoly.models.GameInstance;
import umn.angrypenguins.monopoly.models.PlayerInstance;
import umn.angrypenguins.monopoly.webservice.WebServiceClient;

public class WaitingActivity extends Activity implements View.OnClickListener{
    private GameInstance game;
    private PlayerInstance activePlayer;

    class FindGameTask extends AsyncTask<String, String, GameInstance> {

        @Override
        protected GameInstance doInBackground(String... gameName) {
            return WebServiceClient.findGame( gameName[0] );
        }

        protected void onPostExecute(GameInstance gameInstance) {
            super.onPostExecute(gameInstance);
            
            game = gameInstance;
        }
    }
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.waiting);
        ToolbarConfig toolbarConfig = new ToolbarConfig(this, "Monopoly Money");
        toolbarConfig.getToolbarBackButton().setText( "Quit" );

        Bundle b = this.getIntent().getExtras();
        game = (GameInstance) b.getSerializable("gameInstance");
        activePlayer = (PlayerInstance) b.getSerializable("activePlayer");

        findViewById(R.id.play_button).setOnClickListener(this);
        findViewById(R.id.toolbar_back_button).setOnClickListener(this);
        
        if(activePlayer != null) {
            Log.i("WaitingActivity.onCreate", "found active player, name = " + activePlayer.getName());
            ((TextView)findViewById(R.id.waiting_game_name)).setText(activePlayer.getGame());
            ((TextView)findViewById(R.id.waiting_welcome)).setText("Welcome\n" + activePlayer.getName() + "!");
            
            new FindGameTask().execute( activePlayer.getGame() );
        }
        else {
            Log.i("WaitingActivity.onCreate", "no active player");
            ((TextView)findViewById(R.id.waiting_game_name)).setText(game.getName());
            ((TextView)findViewById(R.id.waiting_welcome)).setText("Welcome\n" + game.getCreator().getName() + "!");
        }
    }

    public void onClick(View view) {
        if (view.getId() == R.id.play_button) {
            Bundle bundle = new Bundle();
            bundle.putSerializable( "gameInstance", game );
            
            if( activePlayer == null )
                bundle.putSerializable("playerInstance", game.getCreator());
            else
                bundle.putSerializable( "playerInstance", activePlayer );
            
            Intent intent = new Intent();
            intent.setClass(this, PlayerActivity.class);
            intent.putExtras(bundle);
            startActivity(intent);
        }
        else if (view.getId() == R.id.toolbar_back_button) {
            Intent intent = new Intent(this, GameView.class);
            startActivity(intent);
        }
    }
}
